<?php

	class Field {
		
		public function __construct() {
		
			$this->Name = "";
			$this->PreviousName = "";
			$this->Type = "";
			$this->Order = 0;
			$this->TeamName = "";
			$this->Description = "";
			$this->Help = "";
					
		}

		function SetName($field_name) {
			$this->PreviousName = $this->Name;
			$this->Name = strtoupper(substr($field_name,0,50));
		}

		function SetType($field_type) {
			$this->Type = strtoupper(substr($field_type,0,50));
		}

		function SetOrder($field_order) {
			$this->Order = $field_order;
		}


		function SetTeamName($team_name) {
			$this->Type = strtoupper(substr($team_name,0,50));
		}

		function SetDescription($field_description) {
			$this->Description = substr($field_description,0,255);
		}

		function SetHelp($field_help) {
			$this->Help = substr($field_help,0,255);
		}
		
		public function GetName() {
			return $this->Name;
		}

		public function GetType() {
			return $this->Type;
		}

		public function GetOrder() {
			return $this->Order;
		}

		public function GetTeamName() {
			return $this->TeamName;
		}

		public function GetDescription() {
			return $this->Description;
		}

		public function GetHelp() {
			return $this->Help;
		}

		function Load($field_name) {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$field_name = strtoupper(substr($field_name,0,50));
	
			mysql_connect($db_host,$db_username,$db_password);
			@mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "SELECT * FROM field WHERE field_name = '$field_name';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			mysql_close();	
			
			if($db_count>0) {
			
				$field_name				= mysql_result($db_result,0,"field_name");
				$field_type				= mysql_result($db_result,0,"field_type");
				$field_order			= mysql_result($db_result,0,"field_order");
				$team_name				= mysql_result($db_result,0,"team_name");
				$field_description		= mysql_result($db_result,0,"field_description");
				$field_help				= mysql_result($db_result,0,"field_help");
				
				$this->Name = strtoupper($field_name);
				$this->Type = strtoupper($field_type);	
				$this->Order = $field_order;
				$this->TeamName = strtoupper($team_name);
				$this->Description = $field_description;	
				$this->Help = $field_help;
				
				return true;			
	
			} else {
			
				$this->Name = "";
				$this->Type = "";
				$this->Order = 0;
				$this->TeamName = "";
				$this->Description = "";
				$this->Help = "";
				
				return false;
				
			}
		
		}
		
		function Save() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$field_name = $this->Name;
			$field_type = $this->Type;
			$field_order = $this->Order;
			$team_name = $this->TeamName;
			$field_description = $this->Description;
			$field_help = $this->Help;
			$field_previous_name = $this->PreviousName;
			
			$field_name = strtoupper(substr($field_name,0,50));
			$field_type = strtoupper(substr($field_type,0,50));			
			$team_name = strtoupper(substr($team_name,0,50));
			$field_description = substr($field_description,0,255);	
			$field_help = substr($field_help,0,255);
					
			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			
			$db_query = "SELECT * FROM field WHERE field_name = '$field_previous_name';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			
			if($db_count==0) {
				
				$db_query = "INSERT INTO field VALUES ('$field_name','$field_type',$field_order,'$team_name','$field_description','$field_help');";
				mysql_query($db_query);
				
			} else {
			
				$db_query = "UPDATE field SET field_name = '$field_name', field_type = '$field_type', field_order = $field_type, team_name = '$team_name', field_description = '$field_description', field_help = '$field_help' WHERE field_name = '$field_previous_name';";
				mysql_query($db_query);
			
			}
			
			mysql_close();
		
		}

		function Delete() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$field_name = $this->Name;			
					
			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "DELETE FROM field WHERE field_name = '$field_name';";
			mysql_query($db_query);
			mysql_close();
		
		}
		
	
	}

?>
